package com.yunchao.library.socket.model.submit

import com.yunchao.library.socket.constants.Limits
import com.yunchao.library.socket.extensions.putString
import com.yunchao.library.socket.utils.BufferPool

data class AuthorRequest(
    val aid: String,
    val users: List<UInt>
) {
    init {
        require(aid.isNotBlank()) { "AID cannot be blank" }
        require(users.size <= Limits.MAX_BYTE_ARRAY_SIZE / 4) {
            "Too many users: ${users.size}"
        }
    }

    fun toByteArray(): ByteArray {
        return BufferPool.use(4 + aid.length + 2 + users.size * 4) { buffer ->
            buffer.apply {
                putString(aid)
                putShort(users.size.toShort())
                users.forEach { putInt(it.toInt()) }
            }
            buffer.array().copyOf(buffer.position())
        }
    }
}